using System;
using Volo.Abp.Domain.Entities;

namespace AuthService.Domain.Security;

/// <summary>
/// 风险评估实体
/// </summary>
public class RiskAssessment : Entity<Guid>
{
    
    /// <summary>
    /// 租户ID
    /// </summary>
    public Guid? TenantId { get; set; }

    /// <summary>
    /// 用户ID
    /// </summary>
    public virtual Guid UserId { get; set; }

    /// <summary>
    /// 评估ID
    /// </summary>
    public virtual string AssessmentId { get; set; } = string.Empty;

    /// <summary>
    /// 操作类型
    /// </summary>
    public virtual string ActionType { get; set; } = string.Empty;

    /// <summary>
    /// 风险分数
    /// </summary>
    public virtual float RiskScore { get; set; }

    /// <summary>
    /// 风险级别
    /// </summary>
    public virtual string RiskLevel { get; set; } = "Low";

    /// <summary>
    /// 是否允许
    /// </summary>
    public virtual bool IsAllowed { get; set; }

    /// <summary>
    /// 拒绝原因
    /// </summary>
    public virtual string RejectionReason { get; set; } = string.Empty;

    /// <summary>
    /// 评估时间
    /// </summary>
    public virtual DateTime AssessedAt { get; set; }

    /// <summary>
    /// IP地址
    /// </summary>
    public virtual string IpAddress { get; set; } = string.Empty;

    /// <summary>
    /// 用户代理
    /// </summary>
    public virtual string UserAgent { get; set; } = string.Empty;

    /// <summary>
    /// 评估详情
    /// </summary>
    public virtual string AssessmentDetails { get; set; } = string.Empty;

    /// <summary>
    /// 备注
    /// </summary>
    public virtual string Remark { get; set; } = string.Empty;

    /// <summary>
    /// 是否已处理
    /// </summary>
    public virtual bool IsProcessed { get; set; }

    /// <summary>
    /// 处理时间
    /// </summary>
    public virtual DateTime? ProcessedTime { get; set; }

    /// <summary>
    /// 处理人
    /// </summary>
    public virtual string ProcessedBy { get; set; } = string.Empty;

    /// <summary>
    /// 过期时间
    /// </summary>
    public virtual DateTime? ExpiresAt { get; set; }

    public RiskAssessment()
    {
        AssessedAt = DateTime.Now;
        IsProcessed = false;
        ExpiresAt = DateTime.Now.AddDays(30); // 默认30天后过期
    }

    public RiskAssessment(Guid userId, string actionType, float riskScore) : this()
    {
        UserId = userId;
        ActionType = actionType;
        RiskScore = riskScore;
        RiskLevel = DetermineRiskLevel(riskScore);
        IsAllowed = RiskLevel != "High";
    }

    /// <summary>
    /// 确定风险级别
    /// </summary>
    private string DetermineRiskLevel(float score)
    {
        return score switch
        {
            >= 0.8f => "High",
            >= 0.5f => "Medium",
            _ => "Low"
        };
    }
}